<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );


/**
 * @package		Joomla
 * @subpackage	JobBoard
 */
class JobBoardTableJA_ManageFields extends JTable
{
	/** @var int Primary key */
	var $id				= null;
	/** @var string */			
	var $form_id     	= null;
 	/** @var string */
 	var $section       	= null;
 	/** @var int */
 	var	$reg_expr		= null;
 	/** @var string */         
 	var $label 			= null;
 	/** @var int */         
 	var $field_type 		= null;
	/** @var int */         
	var $field_ordering  = null;
	/** @var boolean */         
	var $is_required     = null;
	/** @var boolean */         
	var $field_validate_type     = null;
	/** @var string */         
	var $error_message   = null;
	/** @var string */         
	var $field_init      = null;
	/** @var int */         
	var $field_width     = null;
	/** @var int */         
	var $field_height    = null;
	/** @var boolean */         
    var $is_on_search = null;
    /** @var boolean */         
    var $is_on_filter = null;
    /** @var int */         
	var $search_ordering = null;
	/** @var string */         
	var $field_comment   = null;
	/** @var boolean */         
	//var $is_prefill      = null;
    /** @var boolean */         
    var $is_readonly      = null;
	/** @var string */         
	var $field_name      = null;
	/** @var string */         
	var $old_field_name  = null;
	/** @var string */         
	var $old_field_type  = null;
	/** @var string */         
	var $old_table_name  = null;
	/** @var boolean */         
	var $is_filter       = null;
	/** @var int */         
	var $checked_out     = null;
	/** @var datetime */         
	var $checked_out_time= null;
	/** @var string */         
	var $params          = null;
	/** @var string */         
    var $table_name       = null;
    /** @var boolean */         
    var $is_referred       = null;
    /** @var string */         
	var $referred_key       = null;
	/** @var string */         
    var $referred_value       = null;
    /** @var string */         
	var $referred_table       = null;
	/** @var bool */         
    var $field_state       = null;
    /** @var int */         
	var $display       = '';
	 /** @var string */  
	var $field_example       = null;
	var $published       = 0;
	var $is_system       = 0;
	var $eventmouse       = null;
	var $field_class = null;
	var $functionjs = null;
	var $image_width = null;
	var $image_height = null;
	/**
	* @param database A database connector object
	*/
	function __construct(&$db)
	{
		parent::__construct( '#__ja_form_fields', 'id', $db );
	}
	
	/**
	 * Overloaded check function
	 *
	 * @access public
	 * @return boolean
	 * @see JTable::check
	 * @since 1.5
	 */
	function check()
	{
		$filter	= new JFilterInput( '', '', 1, 1);
		$this->label = trim($filter->clean($this->label));
		$this->field_init = $filter->clean($this->field_init);
		$this->error_message = $filter->clean($this->error_message);
		$this->field_example = $filter->clean($this->field_example);
		$this->field_comment = $filter->clean($this->field_comment);
//		JFilterOutput::objectHTMLSafe($this,ENT_QUOTES,'');
		if(empty($this->label))
			return false;
		
		if(empty($this->field_name))
			return false;
		
		if(empty($this->table_name))
			return false;
					
		return true;
			
	}
	
	function bind($from){
	    parent::bind($from);
	    
	    if(!$this->id){
	    	$this->field_state = 'ins';
	    	$this->old_field_name = $this->field_name;
	    	$this->old_field_type = $this->field_type;
	    	$this->old_table_name = $this->table_name;
		} else {
			$query = "SELECT field_name, field_type, table_name, old_field_name, old_field_type, old_table_name FROM ". $this->_tbl 
	    		." WHERE id = ". $this->id ;
		    $this->_db->setQuery($query);
		    
		    $a = $this->_db->loadAssoc();
			
		    if($this->is_system){
		    	$this->field_name = $a["field_name"];
		    	$this->field_type = $a["field_type"];
		    	$this->table_name = $a["table_name"];
		    }
			elseif( ($this->field_name != $a["old_field_name"] && $a["old_field_name"]!='') || 
					($this->field_type != $a["old_field_type"] && $a["old_field_type"]!='') || 
					($this->table_name != $a["old_table_name"] && $a["old_table_name"]!='' )){
	        	$this->field_state = ($this->field_state != 'ins')?'mod' : 'ins';
			}	
			else{
				$this->field_state = ($this->field_state == 'ins')?'ins':'chg';
			}
		} 
	    // exit;
	    if(!$this->id && $this->table_name){
	    	$query = "SELECT MAX(field_ordering) FROM ". $this->_tbl 
	    		." WHERE table_name = ". $this->_db->Quote($this->table_name) 
	    		." GROUP BY table_name";
		    $this->_db->setQuery($query);
		    $maxord = $this->_db->loadResult();
		   
		    if ($this->_db->getErrorNum())
			{
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
	    	$this->field_ordering = $maxord + 1;
		}
		return true;
	}
	
	function reorder(){
		$k = $this->_tbl_key;
		$query = 'SELECT '.$this->_tbl_key.', field_ordering, table_name'
			. ' FROM '. $this->_tbl
			. ' WHERE field_ordering >= 0 '
			. ' ORDER BY table_name, field_ordering'
			;
		$this->_db->setQuery( $query );
		if (!($orders = $this->_db->loadObjectList()))
		{
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		// compact the ordering numbers
		$orderNo = 0;
		$currentTable = '';
		for ($i=0, $n=count( $orders ); $i < $n; $i++, $orderNo++)
		{
			if($currentTable != $orders[$i]->table_name){
				$currentTable = $orders[$i]->table_name;
				$orderNo = 0;
			}
			
			if ($orders[$i]->field_ordering >= 0)
			{
				if ($orders[$i]->field_ordering != $orderNo+1)
				{
					$orders[$i]->field_ordering = $orderNo+1;
					$query = 'UPDATE '.$this->_tbl
					. ' SET field_ordering = '. (int) $orders[$i]->field_ordering
					. ' WHERE '. $k .' = '. $this->_db->Quote($orders[$i]->$k)
					;
					$this->_db->setQuery( $query);
					$this->_db->query();
				}
			}
		}

		return true;
	}
	
	function move($movement){
		$k = $this->_tbl_key;
		
		if($movement > 0){
		    $query = "SELECT id, field_ordering FROM ".$this->_tbl
		    	." WHERE field_ordering > " . $this->field_ordering 
		    	. " ORDER BY field_ordering";
		} else if ($movement < 0){
			$query = "SELECT id, field_ordering FROM ".$this->_tbl
				." WHERE 0 < field_ordering AND field_ordering < " . $this->field_ordering 
				. " ORDER BY field_ordering DESC";
		}
		$this->_db->setQuery($query, 0, 1);
		 
		$row = null;
		$row = $this->_db->loadObject();

		if (isset($row))
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET field_ordering = '. (int) $row->field_ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );
            
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$query = 'UPDATE '.$this->_tbl
			. ' SET field_ordering = '.(int) $this->field_ordering
			. ' WHERE '.$this->_tbl_key.' = '.$this->_db->Quote($row->$k)
			;
			$this->_db->setQuery( $query );
           
			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}

			$this->field_ordering = $row->field_ordering;
		}
		else
		{
			$query = 'UPDATE '. $this->_tbl
			. ' SET field_ordering = '.(int) $this->field_ordering
			. ' WHERE '. $this->_tbl_key .' = '. $this->_db->Quote($this->$k)
			;
			$this->_db->setQuery( $query );

			if (!$this->_db->query())
			{
				$err = $this->_db->getErrorMsg();
				JError::raiseError( 500, $err );
			}
		}
		return true;
	}
}